package com.ruoyi.config;
import com.ruoyi.domain.TtsEventLog;
import com.ruoyi.mapper.TtsEventLogMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AlarmPollingService {
    private final TtsEventLogMapper alarmMapper;
    private final AlarmNotificationService notificationService;
    private Long lastProcessedId = 0L;
    public AlarmPollingService(TtsEventLogMapper alarmMapper,
                               AlarmNotificationService notificationService) {
        this.alarmMapper = alarmMapper;
        this.notificationService = notificationService;
    }
    @Scheduled(fixedRate = 10000) // 测试每10秒执行一次（1秒为1000毫秒）
    public void pollNewAlarms() {
        // 查询新增的报警信息
        List<TtsEventLog> newEvents = alarmMapper.selectNewEvents(lastProcessedId);
        if (!newEvents.isEmpty()) {
            // 处理新报警
            for (TtsEventLog event : newEvents) {
                notificationService.processNewAlarm(event);
            }
            // 更新最后处理的ID
            lastProcessedId = newEvents.get(newEvents.size() - 1).getId();
        }
    }
}